  //编码器
#define ENCODER_CLK_PIN 2//编码器使用中断端口
#define ENCODER_DT_PIN 4
#define ENCODER_SW_PIN 3 //编码器按钮端口


byte int_num=0;
boolean CW_1=0;   // 转向判据1
boolean CW_2=0;   // 转向判据2

#define GPIO_BATTERY_CHECK A1 //电池电压检测
#define POWER_CHECK_PIN 9 //供电电压检测


#include "Chrono.h"
#include "SwitchData.h"
#include "Config.h"
#include "OLED_Display.h"



Config _config;
ASwitch _aSwitch;
SwitchData _switchData;
Chrono TaskCheckGPIO(Chrono::MILLIS);//端口检测
Chrono TaskDisplay(Chrono::MILLIS);//屏幕刷新

OLEDDisplay _display;

//时间计数
long BtnClickTime=0;
byte ECLastState=1;

bool isLock=false;
byte TaskCount=3;

void setup() {  
  //Serial.begin(9600);
  Serial.begin(115200);
  while(!Serial){}
  //TaskDisplay.stop();
  _display.Init();

  attachInterrupt(0, EncoderKey, CHANGE);//中断函数  
  
  pinMode(ENCODER_CLK_PIN,INPUT);  
  pinMode(ENCODER_DT_PIN,INPUT);     
  pinMode(ENCODER_SW_PIN,INPUT);   
  pinMode(POWER_CHECK_PIN,INPUT);  

  

   
  _aSwitch.begin();
  _config.GetConfig();
  
  _aSwitch.SetAnt(_config.ConfigInfo.AntIndex);
  
   
   
  // put your setup code here, to run once:
  
  delay(2000);
  _display.isRef=true;
   
  _display.ShowFrm(1);
  _display.setMode(_config.ConfigInfo.WMode);    
  _display.SetRadioType(_config.ConfigInfo.SDataType);
  _display.setAntIndex(_config.ConfigInfo.AntIndex); 
  _display.setPowerInfo(digitalRead(POWER_CHECK_PIN)==HIGH);
  
  _display.isRef=false;
  _display.oled.display();
  _switchData.RadioType=_config.ConfigInfo.SDataType;

  //TaskDisplay.resume();
  
}


void loop() {
  CheckECKey();
  if(TaskDisplay.hasPassed(300))
  {               
      //_display.oled.display();
      _display.ref_display();
      if(_display.F_Index==1 && _config.ConfigInfo.SDataType>0)
      {
        TaskCount++;
        if(TaskCount>=4)
        {         
          TaskCount=0;
          _switchData.GetData();          
        }
      }
      TaskDisplay.restart();
  }
  
  if(TaskCheckGPIO.hasPassed(2000))
  {      
    if(_display.F_Index==1)
    {
      checkGPIO();
    }
    TaskCheckGPIO.restart();
  }   
}

void checkGPIO(){


  _display.setPowerInfo(digitalRead(POWER_CHECK_PIN)==HIGH);

  if(!_display.isDC)
  {
    float voltage = analogRead(GPIO_BATTERY_CHECK);  
    voltage = voltage * (5.0 / 1023.0)*2;
    _display.setVoltage(voltage);  
  }
}


void serialEvent() 
{
  _switchData.CheckData(DataCallBack);
}

void DataCallBack(byte dataType,byte data[],byte len)
{

  if(dataType==1)
  {  
    switch(data[0]){
      case 1://设置天线        
        SetAntIndex(data[1]);
      break;
      case 2://设置接口名称        
        char cName[5];
        Commend::B2CA(cName,data,2,5);
        if(data[1]==0x00)
        {
          _config.AntName_1=cName;          
        }
        else if(data[1]==0x01)
        {          
          _config.ComName=cName;          
        }
        else{          
          _config.AntName_2=cName;          
        }
        _config.SaveConfig();
      break;
      case 3://协议类型
        _switchData.RadioType=_config.ConfigInfo.SDataType=data[1];
        if(_display.F_Index==1){
          _display.SetRadioType(_config.ConfigInfo.SDataType);
        }
        else{
          _display.RadioType=_config.ConfigInfo.SDataType;
        }
        _config.SaveConfig();
      break;
      case 4://工作模式
        SetModel(data[1]);        
      break;   
      case 5://设置天线端口频率设置
        if(data[1]==0x00)
        {//天线1
          _config.ConfigInfo.AntFreq_1[0]=Commend::HexToInt(data[2],data[3]);
          _config.ConfigInfo.AntFreq_1[1]=Commend::HexToInt(data[4],data[5]);
        }
        else
        {           
          //天线2
          _config.ConfigInfo.AntFreq_2[0]=Commend::HexToInt(data[2],data[3]);
          _config.ConfigInfo.AntFreq_2[2]=Commend::HexToInt(data[4],data[5]);
        }
            
        _config.SaveConfig();
      break;
      case 251://获得设置
        Serial.write(0xFB);        
        Serial.write(_config.ConfigInfo.AntIndex);       
        Serial.write(_config.ConfigInfo.SDataType);  
        Serial.write(_config.ConfigInfo.WMode);
        /*WriteName(_config.AntName_1);
        WriteName(_config.ComName);
        WriteName(_config.AntName_2);*/
        /*Serial.write(dest,2);*/
         
        //Serial.write(_config.ConfigInfo.AntFreq_1[1]);
//        Serial.write(_config.ConfigInfo.AntFreq_2[0]);
        //Serial.write(_config.ConfigInfo.AntFreq_2[1]);
      break;
    }
  
  }
  else if(dataType==2 && _config.ConfigInfo.SDataType>0)
  {
     
    //频率更新
    _display.setFreq(_switchData.FREQ);
     
    if(_config.ConfigInfo.WMode==0 )
    {
      if(_switchData.FREQ>= _config.ConfigInfo.AntFreq_1[0]  
      && _switchData.FREQ <=_config.ConfigInfo.AntFreq_1[1])
      {
        SetAntIndex(0);
      }
      else if(_switchData.FREQ>= _config.ConfigInfo.AntFreq_2[0]  
      && _switchData.FREQ <=_config.ConfigInfo.AntFreq_2[1])
      {SetAntIndex(1);}
    }
  }
}

/*
 * 设置天线接口
*/
void SetAntIndex(byte antIndex){
    _aSwitch.SetAnt(antIndex);
    _config.ConfigInfo.AntIndex=antIndex;
    _config.SaveConfig();
    if(_display.F_Index==1){
      _display.setAntIndex(antIndex);
    }
    else{
      _display.AntIndex=antIndex;
    }
}
/*
 * 设置工作模式
*/
void SetModel(byte val)
{
  _config.ConfigInfo.WMode=val;
  if(_display.F_Index==1){
    _display.setMode(_config.ConfigInfo.WMode);  
  }
  else{
    _display.Mode=val;
  }
  _config.SaveConfig();
}

void WriteName(String c_name){
  byte nLen=c_name.length();
  for(byte i=0;i<5;i++)
  {
    if(i<nLen)
    {
      Serial.write(_config.AntName_1.charAt(i));  
    }
    else
    {Serial.write(0x00);  }
    
  }
}

void EncoderKey(){
 
 if (int_num == 0 && digitalRead(ENCODER_CLK_PIN) == LOW) {
    CW_1 = digitalRead(ENCODER_DT_PIN);
    int_num = 1;
  }
  
  if (int_num == 1 && digitalRead(ENCODER_CLK_PIN) == HIGH) {
     CW_2 = !digitalRead(ENCODER_DT_PIN);
    if (CW_1 == true && CW_2 == true) {      
        if(_display.F_Index==1)
       {          
        if(isLock)return;
        SetModel(1);
         _display.setAntIndex(0);         
         _aSwitch.SetAnt(0);
       }
       else if(_display.F_Index==2)
       {
          _display.SelectMenu(false);  
       }
    }
    else if (CW_1 == false && CW_2 == false) {      
       if(_display.F_Index==1)
       {           
        if(isLock)return;
          _display.setAntIndex(1);          
          _aSwitch.SetAnt(1);
       }
       else if(_display.F_Index==2)
       {
          _display.SelectMenu(true);  
       }
    }
    int_num = 0;
  }
}




//EC按键判断
void CheckECKey(){   

  if(ECLastState==digitalRead(ENCODER_SW_PIN))return;

  ECLastState=digitalRead(ENCODER_SW_PIN);
  if(digitalRead(ENCODER_SW_PIN)==LOW)
  {
    BtnClickTime = millis();  
  }
  else
  {
    BtnClickTime=millis() - BtnClickTime;
     
    if(BtnClickTime>1000)   
    {     
      if(_display.F_Index==1)
      {
        isLock=!isLock;
        _display.SetLock(isLock);
      }
    
    }
    else{   
     if(isLock)return;     
     if(_display.F_Index==1)
     { 
        _display.ShowMenu(0);
     }
     else if(_display.F_Index==2)
     {
        _display.MenuClick(DataCallBack);
     }
    }
  }
}
